package com.marktim.pokerpoker.menu;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;
import android.widget.ImageButton;

import com.marktim.pokerpoker.R;
import com.marktim.pokerpoker.game.ClientGameActivity;
import com.marktim.pokerpoker.game.ServerGameActivity;

public class MenuActivity extends FragmentActivity {

	private static final int PAGE_MEMORY = 2;
	private static final Point SCREEN = new Point();
	private static final int SCALE_ANIMATION_DURATION = 550;

	private boolean portrait;
	private boolean phone;

	private SectionsPagerAdapter sectionsPagerAdapter;
	private ViewPager menuPager;
	private ImageButton actionButton;
	private View actionButtonClickTarget;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		WindowManager wm = ((WindowManager) getSystemService(Context.WINDOW_SERVICE));
		Display d = wm.getDefaultDisplay();
		d.getSize(SCREEN);

		phone = !getResources().getBoolean(R.bool.isTablet);
		portrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;

		sectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

		menuPager = (ViewPager) findViewById(R.id.pager);
		menuPager.setPageMargin(getResources().getDimensionPixelSize(R.dimen.pageMargin));
		menuPager.setOffscreenPageLimit(PAGE_MEMORY);
		menuPager.setAdapter(sectionsPagerAdapter);

		actionButton = (ImageButton) findViewById(R.id.actionButton);
		actionButton.setScaleX(0);
		actionButton.setScaleY(0);
		actionButton.animate().scaleX(1).scaleY(1).setInterpolator(new AccelerateInterpolator()).setDuration(SCALE_ANIMATION_DURATION)
				.start();

		actionButtonClickTarget = (Button) findViewById(R.id.actionButtonDummy);
		if (actionButtonClickTarget == null) {
			actionButtonClickTarget = actionButton;
		}

		actionButtonClickTarget.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (menuPager.getCurrentItem()) {
				case MenuSectionFragment.PAGE_CREATE: 
					startServer();
					break;
				case MenuSectionFragment.PAGE_JOIN: 
					joinGame();
					break;
				}
			}
		});

		menuPager.setOnPageChangeListener(portrait && phone ? verticalPagerListener : horizontalPagerListener);
	}
	
	protected void joinGame() {
		Intent i = new Intent(this, ClientGameActivity.class);
		startActivity(i);
	}

	protected void startServer() {
		Intent i = new Intent(this, ServerGameActivity.class);
		startActivity(i);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu, menu);
		return true;
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:
			menuPager.setCurrentItem(SectionsPagerAdapter.SETTINGS_PAGE);
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	public class SectionsPagerAdapter extends FragmentPagerAdapter {

		private static final int PAGE_COUNT = 3;
		private static final int SETTINGS_PAGE = 2;

		public SectionsPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {
			Fragment fragment = new MenuSectionFragment();

			Bundle args = new Bundle();
			args.putInt(MenuSectionFragment.ARG_SECTION_NUMBER, position);
			fragment.setArguments(args);

			return fragment;
		}

		@Override
		public int getCount() {
			return PAGE_COUNT;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "";
		}
	}
	private OnPageChangeListener horizontalPagerListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {
			actionButtonClickTarget.setVisibility(View.VISIBLE);
		}

		@Override
		public void onPageScrolled(int page, float openPercentage, int arg2) {

			actionButtonClickTarget
					.setVisibility(openPercentage != 0 && actionButtonClickTarget.getVisibility() == View.VISIBLE ? View.GONE
							: View.VISIBLE);

			// hide button if in settings
			actionButton.setVisibility(page == 1 && openPercentage > .5f || page == 2 ? View.GONE : View.VISIBLE);

			// set translation
			if (openPercentage < .57f) {
				actionButton.setImageResource(page == 0 ? R.drawable.ic_action_content_new : R.drawable.ic_action_navigation_forward);
				actionButton.setTranslationX(-.9f * openPercentage * SCREEN.x);

			} else {
				actionButton.setImageResource(page == 1 ? R.drawable.ic_action_content_new : R.drawable.ic_action_navigation_forward);
				actionButton.setTranslationX(.4f * (SCREEN.x - openPercentage * SCREEN.x));
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
		}
	};
	private OnPageChangeListener verticalPagerListener = new OnPageChangeListener() {

		@Override
		public void onPageSelected(int arg0) {

		}

		@Override
		public void onPageScrolled(int page, float openPercentage, int arg2) {
			// hide button if in settings
			actionButton.setVisibility(page == 1 && openPercentage > .5f || page == 2 ? View.GONE : View.VISIBLE);

			if (openPercentage < .5) {
				actionButton.setImageResource(page == 0 ? R.drawable.ic_action_content_new : R.drawable.ic_action_navigation_forward);
				actionButton.setScaleX(1 - 2 * openPercentage);
				actionButton.setScaleY(1 - 2 * openPercentage);
			} else {
				actionButton.setImageResource(page == 1 ? R.drawable.ic_action_content_new : R.drawable.ic_action_navigation_forward);
				actionButton.setScaleX(2 * (openPercentage - .5f));
				actionButton.setScaleY(2 * (openPercentage - .5f));
			}
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}
	};
}
